﻿Imports TBCC.UtilityMobile
Public Class ProjectManageForm

    Public Update_Flag As Boolean = False


    Private Sub ProjectManageForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.AutoGenerateColumns = False
        Me.LoadProjects()
    End Sub

    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        Dim frm As New ProjectBasicInfoForm()
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Update_Flag = True '参数发生修改
            LoadProjects()
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        Dim pi As ProjectItem
        pi = getSelectedItem()
        If pi Is Nothing Then
            msg.Show("请选择要删除的终端。")
            Exit Sub
        End If

        'If pi.ProjectID = MainForm.pi.ProjectID And g_offline = False Then
        '    msg.Show("该工程正在连接状态，请先断开连接再删除")
        '    Exit Sub
        'End If

        Dim tip As String
        tip = String.Format("是否要删除终端【{0}】的所有配置信息？", pi.ProjectName)
        If msg.ShowYesNo(tip) = Windows.Forms.DialogResult.Yes Then
            If ProjectService.Delete(pi, ProjectService.DevTypeMaxNetid(pi.ProjectID)) = True Then
                Update_Flag = True '参数发生修改
                Me.LoadProjects()
                msg.Show("删除成功。")
            Else
                msg.ShowError("删除失败。")
            End If
        End If


    End Sub

    ''' <summary>
    ''' 获取当前选择的工程
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function getSelectedItem() As ProjectItem
        If Me.DataGridView1.CurrentRow Is Nothing Then
            Return Nothing
        End If

        Dim pi As ProjectItem
        pi = CType(Me.DataGridView1.CurrentRow.DataBoundItem, ProjectItem)
        Return pi

    End Function

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim pi As ProjectItem
        pi = getSelectedItem()
        If pi Is Nothing Then
            msg.Show("请选择要修改的终端。")
            Exit Sub
        End If

        Dim frm As New ProjectBasicInfoForm(pi)
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Update_Flag = True '参数发生修改
            LoadProjects()
        End If

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub LoadProjects()
        Dim l As IList(Of ProjectItem)

        l = ProjectService.GetProjectList()
        Me.DataGridView1.DataSource = Nothing
        Me.DataGridView1.DataSource = l

        If l.Count = 0 Then
            Me.btnUpdate.Enabled = False
            Me.btnDelete.Enabled = False
        Else
            Me.btnUpdate.Enabled = True
            Me.btnDelete.Enabled = True
        End If

    End Sub

    Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
        Me.btnUpdate.PerformClick()
    End Sub

End Class